package ar.com.solidussource.howmuchservices.utils.mail;

import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
 
public class SendMail {
	private static String MAIL_SMTP_HOST = "smtp.gmail.com";
	private static String MAIL_SMTP_PORT = "587";
	private static String MAIL_SMTP_AUTH = "true";
	private static String MAIL_SMTP_STARTTLS_ENABLE = "true";
	private static String MAIL_SMTP_PASSWORD = "";
 
	private String from;
	private String to;
	private String subject;
	private String text;
 
	public SendMail(String from, String to, String subject, String text){
		this.from = from;
		this.to = to;
		this.subject = subject;
		this.text = text;
	}
 
	public void send() throws AddressException, MessagingException {
 
		Properties props = new Properties();
		props.put("mail.smtp.host", MAIL_SMTP_HOST);
		props.put("mail.smtp.port", MAIL_SMTP_PORT);
		props.put("mail.smtp.auth", MAIL_SMTP_AUTH);
		props.put("mail.smtp.starttls.enable", MAIL_SMTP_STARTTLS_ENABLE);
		
		Session mailSession = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
					return new PasswordAuthentication(from, MAIL_SMTP_PASSWORD);
			}
		});
 
		InternetAddress fromAddress = new InternetAddress(from);
		InternetAddress toAddress = new InternetAddress(to);
		
		Message simpleMessage = new MimeMessage(mailSession);
 
		simpleMessage.setFrom(fromAddress);
		simpleMessage.setRecipient(RecipientType.TO, toAddress);
		simpleMessage.setSubject(subject);
		simpleMessage.setText(text);
 
		Transport.send(simpleMessage);
	}
}